<!-- 车辆管理 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="车辆牌照" prop="vehicleNumber">
        <el-input
          v-model="queryParams.vehicleNumber"
          placeholder="请输入车辆牌照号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌颜色" prop="vehiclePlateColorCode">
        <el-select v-model="queryParams.vehiclePlateColorCode" placeholder="请选择车牌颜色" clearable size="small">
          <el-option
            v-for="item in vehiclePlateColorOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="认证状态" prop="isCert">
        <el-select v-model="queryParams.isCert" placeholder="请选择认证状态" clearable size="small">
          <el-option
            v-for="item in isCertOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tws:vehicle:export']"
        >导出</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-badge :value="isCertNum">
          <el-button
            type="success"
            plain
            icon="el-icon-coordinate"
            size="mini"
            @click="vehicleCert"
            v-hasPermi="['tws:vehicle:edit']"
          >车辆审核</el-button>
        </el-badge>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleUpload"
          v-hasPermi="['tws:vehicle:edit']"
        >上传</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="操作" align="center" width="110" >
        <template slot-scope="scope">
          <el-button v-if="scope.row.isCert == $store.state.constants.isCert_yrz"
            style="color: #67c23a;"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tws:vehicle:edit']"
          >修改</el-button>
          <el-button v-if="scope.row.isCert == $store.state.constants.isCert_yrz"
            style="color: red;"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tws:vehicle:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
      <el-table-column label="车辆牌照号" align="center" width="110">
        <span slot="header">
          车辆牌照号
          <el-tooltip effect="dark" placement="top" content="点击车辆牌照号可查看详情">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </span>
        <template slot-scope="scope">
          <span style="color: #1890ff; cursor: pointer;" @click="handleView(scope.row)">{{ scope.row.vehicleNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车牌颜色" align="center" prop="vehiclePlateColorCode" :formatter="vehiclePlateColorFormat" />
      <el-table-column label="车辆类型" align="center" prop="vehicleType" :formatter="vehicleTypeFormat" min-width="120" show-overflow-tooltip />
      <el-table-column label="能源类型" align="center" prop="vehicleEnergyType" :formatter="vehicleEnergyTypeFormat" />
      <el-table-column label="核载(千克)" align="center" prop="vehicleTonnage" />
      <el-table-column label="吨位(千克)" align="center" prop="grossMass" />
      <el-table-column label="道路运输证号" align="center" prop="roadTransportCertificateNumber" width="130" />
      <!-- <el-table-column label="速记码" align="center" prop="shortCode" /> -->
      <el-table-column label="所有人" align="center" prop="owner" />
      <el-table-column label="使用性质" align="center" prop="useCharacter" />
      <el-table-column label="识别代号" align="center" prop="vin" show-overflow-tooltip />
      <el-table-column label="发证机关" align="center" prop="issuingOrganizations" show-overflow-tooltip />
      <el-table-column label="注册日期" align="center" prop="registerDate" width="110" />
      <el-table-column label="发证日期" align="center" prop="issueDate" width="110" />
      <el-table-column label="认证状态" align="center" prop="isCert" :formatter="isCertFormat" />
      <!-- <el-table-column label="状态" align="center" width="140" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="2"
            active-text="正常"
            inactive-text="禁用"
            @change="changeState(scope.row)" :disabled="scope.row.isCert !== $store.state.constants.isCert_yrz">
          </el-switch>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body :close-on-click-modal="false">
      <vehicle-form :mode="mode" :vehicleId="vehicleId" @close="close" />
    </el-dialog>

    <!-- 上传进度对话框 -->
    <el-dialog title="正在上传，请稍候..." :visible.sync="openUpload" width="500px" append-to-body :close-on-click-modal="false" @close="close">
      <el-progress style="margin-left: 160px;" v-if="percentage >= 100" type="circle" :percentage="100" status="success"></el-progress>
      <el-progress style="margin-left: 160px;" v-else type="circle" :percentage="percentage"></el-progress>
    </el-dialog>

  </div>
</template>

<script>
import { listVehicle, delVehicle, getCertNum, stateChange, uploadVehicle } from "@/api/tws/vehicle";
import VehicleForm from '@/components/VehicleForm/index';

export default {
  name: "Vehicle",
  components: {
    VehicleForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      mode: '3',
      vehicleId: null,
      isCertNum: 0,
      percentage: 0,
      // 车牌颜色字典
      vehiclePlateColorOptions: [],
      // 车辆能源类型字典
      vehicleEnergyTypeOptions: [],
      // 车辆类型字典
      vehicleTypeOptions: [],
      // 认证状态字典
      isCertOptions: [],
      // 车辆信息表格数据
      vehicleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openUpload: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        vehicleNumber: null,
        vehiclePlateColorCode: null,
        vehicleType: null,
        vehicleEnergyType: null,
        vehicleTonnage: null,
        grossMass: null,
        roadTransportCertificateNumber: null,
        shortCode: null,
        driverId: null,
        owner: null,
        useCharacter: null,
        vin: null,
        trailerVehiclePlateNumber: null,
        issuingOrganizations: null,
        registerDate: null,
        issueDate: null,
        isCert: null,
        state: null,
      }
    };
  },
  created() {
    this.getDict();
    this.getList();
    this.getCertNum();
  },
  methods: {
    /** 查询字典 */
    getDict() {
      // 车牌颜色字典
      this.getDicts("vehicle_plate_color").then(response => {
        this.vehiclePlateColorOptions = response.data;
      });
      // 车辆能源类型字典
      this.getDicts("vehicle_energy_type").then(response => {
        this.vehicleEnergyTypeOptions = response.data;
      });
      // 车辆类型字典
      this.getDicts("vehicle_type").then(response => {
        this.vehicleTypeOptions = response.data;
      });
      // 认证状态字典
      this.getDicts("is_cert").then(response => {
        this.isCertOptions = response.data;
      });
    },
    /** 查询车辆信息列表 */
    getList() {
      this.loading = true;
      listVehicle(this.queryParams).then(response => {
        this.vehicleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询待审核司机数量 */
    getCertNum() {
      getCertNum().then(res => {
        this.isCertNum = res.data;
      })
    },
    // 车牌颜色字典翻译
    vehiclePlateColorFormat(row, column) {
      return this.selectDictLabel(this.vehiclePlateColorOptions, row.vehiclePlateColorCode);
    },
    // 车辆能源类型字典翻译
    vehicleEnergyTypeFormat(row, column) {
      return this.selectDictLabel(this.vehicleEnergyTypeOptions, row.vehicleEnergyType);
    },
    // 认证状态字典翻译
    isCertFormat(row, column) {
      return this.selectDictLabel(this.isCertOptions, row.isCert);
    },
    // 车辆类型字典翻译
    vehicleTypeFormat(row, column) {
      return this.selectDictLabel(this.vehicleTypeOptions, row.vehicleType);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 停启用
    changeState(row) {
      this.$confirm('确定要' + (row.state == "1" ? '启用' : '禁用') + '该车辆吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        let obj = {
          id: row.id,
          state: row.state
        }
        return stateChange(obj);
      }).then(() => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(err => {
        this.getList();
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id;
      this.open = true;
      this.title = "修改车辆";
      this.mode = '';
      this.vehicleId = null;
      setTimeout(() => {
        this.mode = '2';  // 修改模式
        this.vehicleId = id;
      })
    },
    // 查看详情
    handleView(row) {
      const id = row.id;
      this.open = true;
      this.title = "车辆详情";
      this.mode = '';
      this.vehicleId = null;
      setTimeout(() => {
        this.mode = '3';  // 查看模式
        this.vehicleId = id;
      })
    },
    close() {
      this.open = false;
      this.getList();
    },
    // 跳转车辆审核页面
    vehicleCert() {
      this.$router.push({ path: 'vehicleCert', replace: true });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除车辆 "' + row.vehicleNumber + '" ?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delVehicle(ids);
        }).then(() => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
        }).catch(err => {
        })
    },
    // 上传省平台
    handleUpload() {
      const ids = this.ids;
      let that = this;
      this.$confirm('是否确认上传至省平台?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        that.openUpload = true;
        that.up(ids, 0);
      }).catch(err => {
        that.getList();
      })
    },
    up(ids, ind) {
      let that = this;
      let len = ids.length;
      uploadVehicle(ids[ind]).then(res => {
        ind++;
        that.percentage = Number(((ind / len) * 100).toFixed(1));
        if(ind >= len) {
          setTimeout(() => {
            that.$modal.msgSuccess("上传成功");
            that.openUpload = false;
          }, 1000)
        } else {
          that.up(ids, ind);
        }
      }).catch(err => {
        that.$modal.msgError(err.msg || "上传失败");
      })
    },
    closeUp() {
      this.percentage = 0;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tws/vehicle/export', {
        ...this.queryParams
      }, `车辆.xlsx`)
    }
  }
};
</script>
